﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.DataAccess;

namespace University.Workshop.Business
{
    public class ManagerExpenseControl
    {
        public static ICollection<Model.ExpenseControl> SearchOutFlowByDates(DateTime? initialDate, DateTime? endDate, ref decimal totalMoney)
        { 
            if(initialDate == null)
                initialDate = DateTime.MinValue;
            if(endDate == null)
                endDate =DateTime.MaxValue;
            ICollection<Model.ExpenseControl> otherExpenseControl = DataAccessExpense.GetExpenseBetweenDates(initialDate.Value, endDate.Value);
            ICollection<Model.ExpenseControl> waterExpenseControl = DataAccessWaterPurchase.GetExpenseBetweenDates(initialDate.Value, endDate.Value);

            otherExpenseControl.ToList().ForEach(i => i.StringExpenseType = ((Model.ExpenseType)(i.ExpenseType)).ToString());
            waterExpenseControl.ToList().ForEach(i => i.StringExpenseType = ((Model.WaterExpenseType)(i.ExpenseType)).ToString());

            ICollection<Model.ExpenseControl> genericList = otherExpenseControl.Union(waterExpenseControl).ToList();
            if (genericList.Count == 0)
                throw new Exception("no existen datos registrados");

            totalMoney = genericList.AsEnumerable().Sum(g => g.expensePaymentAmount);
            return (from c in genericList
                    orderby c.SpendDate ascending
                    select c).ToList();

        }
    }
}
